package com.jt.controller;

import com.jt.pojo.Role;
import com.jt.pojo.RoleRights;
import com.jt.service.RightsService;
import com.jt.service.RoleService;
import com.jt.vo.SysResult;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @Autowired
    private RightsService rightsService;

    @GetMapping("/getRoleList")
    public SysResult getRoleList()
    {
        List<Role> roleList=roleService.getRoleList();
        return  SysResult.success(roleList);
    }

    @PostMapping("/submitRights")
    public SysResult saveRights(Integer roleId,Integer... rightsIds){

        roleService.saveRights(roleId,rightsIds);
        return SysResult.success();
    }

    @PostMapping("/addRole")
    public  SysResult addRole(@RequestBody Role role)
    {
           role.setRoleStatus(true);
            roleService.addRole(role);
           return  SysResult.success();
    }


    @DeleteMapping("/deleteRoleRights")
    public  SysResult deleteRoleRights(Integer roleId,Integer rightsId,Integer level)
    {
        //要求删除完成之后,返回最新的权限列表--返回一个Role对象,将权限封装在rightsList属性里
        Role role=roleService.deleteRoleRights(roleId,rightsId,level);
        return  SysResult.success(role);
    }

    @GetMapping("/getRole/{roleId}")
    public SysResult getRole(@PathVariable Integer roleId)
    {
        Role role=roleService.getRole(roleId);
        return  SysResult.success(role);
    }


    @PutMapping("/upRole")
    public  SysResult upRole(@RequestBody Role role)
    {
        roleService.upRole(role);
        return  SysResult.success();
    }

    @DeleteMapping("/deleteRoleById")
    public  SysResult deleteRoleById(Integer roleId)
    {
        roleService.deleteRoleById(roleId);
        return SysResult.success();
    }



}
